const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
    imgHost: app.imgHost,
    home: {}
  },

  onLoad(){
    this.getHome();
  },

  onShareAppMessage(){

  },

  search(){
    console.log('yes');
    wx.switchTab({
      url: '/pages/tabbar/commodity/commodity'
    })
  },

  getRecommend(e){
    let item = e.currentTarget.dataset.item;

    app.appData.commodityItem = item;

    wx.switchTab({
      url: '/pages/tabbar/commodity/commodity'
    })
  },

  getDiscountCoupon(e) {
    let ed = e.currentTarget.dataset;

    AJAX.getDiscountCoupon({
      loadType: 'default',
      loadMsg: '领取中...',
      id: ed.id
    }).then(([status, res, msg]) => {
      $wx.showToast(msg);
    });
  },
  
  getHome(params = {}){
    let _this = this,
    td = _this.data;
    params.loadType = 'bar';

    AJAX.home(params).then(([status, res, msg]) => {
      if(status){
        res.couponList.map(item => {
          item.reduce_price = parseInt(item.reduce_price)
        });

        _this.setData({
          home: res
        });
      }else{
        $wx.showToast(msg);
      }
    });
  }
});